<?php
class Twitter_widget extends WP_Widget 
{
  
  	function Twitter_widget() {	
  		$widget_ops = array( 'classname' => 'Twitter_widget', 'description' => __( " Display list of tweets for given user. This widget is only for footer area." , 'locality')  );
  		$this->WP_Widget( 'Twitter_widget', 'Locality: Footer Twiter Widget', $widget_ops);
  	}
  	
  	function widget($args,  $instance) {
			extract($args);
			
			$title = apply_filters('widget_title', $instance['title']);		
			if ( empty($title) ) 
					$title = false;
			
			$twitter_id =  $instance['twitter_id'];
			$tweets_count =  $instance['tweets_count'];
			
			?>
	          	<div class="footer-widget">
	                <?php
					if($title):
						echo $before_title;
							echo $title;
						echo $after_title;	
					endif;
			
					echo '<ul id="twitter_update_list"><li>'.__('No. Tweets Loaded !', 'locality').'</li></ul>';
						
					?>
						<script src="http://twitterjs.googlecode.com/svn/trunk/src/twitter.min.js" type="text/javascript"></script>
						<script type="text/javascript" charset="utf-8">
							getTwitters('twitter_update_list', { 
								  id: '<?php echo $twitter_id; ?>', 
								  count: <?php echo $tweets_count; ?>,
								  enableLinks: true, 
								  ignoreReplies: true, 
								  clearContents: true,
								  template: '%text%'
							});
						</script>					
				</div>
          	<?php
	}
	

	function form($instance) 
	{			
		$instance = wp_parse_args( (array) $instance, array( 'title' => 'Twitter Feed', 'twitter_id' => 'envato', 'tweets_count' => 3 ) );
		
        $title= esc_attr($instance['title']);
		
		$twitter_id =  $instance['twitter_id'];
		$tweets_count =  $instance['tweets_count'];
		
		?>
				<p>
			            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title', 'locality'); ?></label>
			            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
		        </p>
	            <p>
			            <label for="<?php echo $this->get_field_id('twitter_id'); ?>"><?php _e('Twitter ID', 'locality'); ?></label>
			            <input class="widefat" id="<?php echo $this->get_field_id('twitter_id'); ?>" name="<?php echo $this->get_field_name('twitter_id'); ?>" type="text" value="<?php echo $twitter_id; ?>" />
		        </p>
	            <p>
			            <label for="<?php echo $this->get_field_id('tweets_count'); ?>"><?php _e('Number of Tweets', 'locality'); ?></label>
			            <input class="widefat" id="<?php echo $this->get_field_id('tweets_count'); ?>" name="<?php echo $this->get_field_name('tweets_count'); ?>" type="text" value="<?php echo $tweets_count; ?>" />
		        </p>
		<?php
	}

	function update($new_instance, $old_instance) 
	{
	        $instance=$old_instance;		
			$instance['title'] = strip_tags($new_instance['title']);
	        $instance['twitter_id'] = strip_tags($new_instance['twitter_id']);
			$instance['tweets_count'] = intval($new_instance['tweets_count']);
			
	        return $instance;
    }
  
  }